package com.ruoyi.web.controller.excelImport;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.excelImport.domain.BusTest;
import com.ruoyi.excelImport.service.IBusTestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 测试导入模块Controller
 *
 * @author chentao
 * @date 2022-09-04
 */
@RestController
@RequestMapping("/excelImport/test")
public class BusTestController extends BaseController
{
    @Autowired
    private IBusTestService busTestService;

    /**
     * 查询测试导入模块列表
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusTest busTest)
    {
        startPage();
        List<BusTest> list = busTestService.selectBusTestList(busTest);
        return getDataTable(list);
    }

    /**
     * 导出测试导入模块列表
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:export')")
    @Log(title = "测试导入模块", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusTest busTest)
    {
        List<BusTest> list = busTestService.selectBusTestList(busTest);
        ExcelUtil<BusTest> util = new ExcelUtil<BusTest>(BusTest.class);
        util.exportExcel(response, list, "测试导入模块数据");
    }

    /**
     * 导入 测试导入模块列表
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:export')")
    @Log(title = "测试导入模块", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<BusTest> util = new ExcelUtil<BusTest>(BusTest.class);
        List<BusTest> busTests = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = busTestService.importData(busTests, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导入模板下载
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<BusTest> util = new ExcelUtil<BusTest>(BusTest.class);
        util.importTemplateExcel(response, "导入数据");
    }


    /**
     * 获取测试导入模块详细信息
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(busTestService.getById(id));
    }

    /**
     * 新增测试导入模块
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:add')")
    @Log(title = "测试导入模块", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusTest busTest) {
        return toAjax(busTestService.save(busTest));
    }

    /**
     * 修改测试导入模块
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:edit')")
    @Log(title = "测试导入模块", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusTest busTest) {
        return toAjax(busTestService.updateById(busTest));
    }

    /**
     * 删除测试导入模块
     */
    @PreAuthorize("@ss.hasPermi('excelImport:test:remove')")
    @Log(title = "测试导入模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(busTestService.removeByIds(Arrays.asList(ids)));
    }
}
